package com.example.demo.service.equipment;

import com.example.demo.domain.equipment.Equipment;
import com.example.demo.domain.equipment.repository.EquipmentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EquipmentService {
    @Autowired
    private EquipmentRepository equipmentRepository;
    private String plan_id;

    /**
     * @Description: 获取全部未审批列表
     * @Param:  无
     * @return:  全部未审批列表
     * @Author: wangze
     */
    public List<Equipment> getEquipment() {
        return equipmentRepository.getAllByTicketIdIsNotNull();
    }

    /**
     * @Description: 读取equipment页面传来的planId
     * @Param:  无
     * @return:  全部未审批列表
     * @Author: wangze
     */
    public void getPlanid(Equipment e) {
        plan_id =  e.getPlanId();
    }

    /**
     * @Description: 转换为plan_id便于budget页面使用
     * @Param:  无
     * @return:  全部未审批列表
     * @Author: wangze
     */
    public String getPlan_id(){
        return plan_id;
    }

    public Equipment addequip(Equipment e) {
        String d = e.getDate();
        e.setDate(d);
        e.setState(0);
        return equipmentRepository.save(e);
    }

    //返回已通过列表
    public List<Equipment> getPassEquipment(){
        return equipmentRepository.findByState(1);
    }

    //返回未通过列表
    public List<Equipment> getRejectEquipment(){
        return equipmentRepository.findByState(-1);
    }

    //返回待审批列表
    public List<Equipment> getWaitEquipment(){
        return equipmentRepository.findByState(0);
    }

    //通过审核
    public Equipment passEquipment(Equipment e){
        Equipment equipment = equipmentRepository.findByTicketId(e.getTicketId());
        equipment.setState(1);
        return equipmentRepository.save(equipment);
    }

    //驳回审核
    public Equipment rejectEquipment(Equipment e){
        Equipment equipment = equipmentRepository.findByTicketId(e.getTicketId());
        equipment.setState(-1);
        return equipmentRepository.save(equipment);
    }
}
